Components.utils.import("resource://siloseer_gui/siloseer_shared.js");

//any changes here, one has to restart browser

var mainWindowOverlay = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                     .getInterface(Components.interfaces.nsIWebNavigation)
                     .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                     .rootTreeItem
                     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                     .getInterface(Components.interfaces.nsIDOMWindow);

//only for Navigation links
function select_element(){
			
	var clickedNode=gContextMenu.target;
	var sidebarDoc= document.getElementById("sidebar").contentDocument;   			
	//alert('global sel links is ' + global_select.nav_val);
	
	if(global_select.type==0)
	{
		if(clickedNode.tagName == 'A')
		{
			navigationSelection(clickedNode,sidebarDoc);
			LOG('You clicked a link node');
		}
		else
		{
			LOG('You did NOT click a link node');
		}
		
	}
	else if(global_select.type==1)
	{
		contentSelection(clickedNode,sidebarDoc);
	}
	
	//Can be later commented out
	nodeHash[global_select.domain][generateXPath(clickedNode)] = mainWindowOverlay.content.document.location.href;

        
}		


function contentSelection(clickedNode,sidebarDoc) {
		
	//var nodes = sidebarDoc.getElementById('con_label_num');
	//alert('nodes len is ' + nodes.length);
	
	//working
	//var links_num = sidebarDoc.getElementById("con_label_num"+global_select.con_val);	
	//alert('links num content is  ' + links_num.textContent);
	
	//var nodes = [];
	//var tmpNode = sidebarDoc.getElementById("con_label_num");	
	 //while(tmpNode){
		//nodes.push(tmpNode);
		//tmpNode.id = "";
		//tmpNode = document.getElementById("con_label_num");
	  //}
	  //for(var x=0; x<nodes .length; x++){
		//nodes[x].id = "con_label_num";
	  //}		
	  
	  //alert('val is ' + nodes[global_select.con_val].textContent);
			
		
	//var cloneReadRootNodes = elementsById('clonereadroot',sidebarDoc);
	//var cloneNodes = cloneReadRootNodes[global_select.con_val].childNodes;
	
	//alert('clone len is ' + cloneReadRootNodes.length);
	
	var con_num = sidebarDoc.getElementById("con_label_num"+global_select.con_val);	
	var my_array= new Array();
	
	my_array['dom'] = clickedNode;
	my_array['url']= mainWindowOverlay.content.document.location.href;
	
	//highlight in page
	$(clickedNode).css("backgroundColor", "#ff3fff");
	
	//store node
	//domains[global_select.domain]['data'][global_select.con_val]['nodes'].push(clickedNode);
	domains[global_select.domain]['data'][global_select.con_val]['nodes'].push(my_array); // new - store url
	
	con_num.textContent = 'Tags: ' + domains[global_select.domain]['data'][global_select.con_val]['nodes'].length;

	//startUrls[global_select.domain]['contentUrls'].push(mainWindowOverlay.content.document.location.href);

}	

function navigationSelection(clickedNode,sidebarDoc) {
	
	var links_num;	
	var my_array= new Array();	
	
	//Content
	if(global_select.nav_val==0)
	{
		//update GUI screen
		links_num = sidebarDoc.getElementById("content_links_num");
		
		my_array['dom'] = clickedNode;
		my_array['url']= mainWindowOverlay.content.document.location.href;			
		
		//highlight in page
		$(clickedNode).css("backgroundColor", "#ff3fff");
		//store node
		//domains[global_select.domain]['links']['content'].push(clickedNode);
		domains[global_select.domain]['links']['content'].push(my_array);
		
		links_num.textContent = domains[global_select.domain]['links']['content'].length;
		//alert('links num val is ' + links_num.textContent);
	}
	else if(global_select.nav_val==1) // Navigation links
	{
		//update GUI screen
		links_num = sidebarDoc.getElementById("nav_links_num");
		
		my_array['dom'] = clickedNode;
		my_array['url']= mainWindowOverlay.content.document.location.href;				
		
		$(clickedNode).css("backgroundColor", "#ff3fff");
		//domains[global_select.domain]['links']['nav'].push(clickedNode);	
		domains[global_select.domain]['links']['nav'].push(my_array);
			
		links_num.textContent = domains[global_select.domain]['links']['nav'].length;	
		//alert('links num val is ' + links_num.textContent);
	}	
}	

/*
function hilite()
{
	var arrayz = domains[global_select.domain]['links']['content'];
	
	alert(arrayz.length);
	
	for(var i in arrayz)
	{
		var c_n = arrayz[i];
		alert(c_n);
		$(c_n).css("backgroundColor", "#ff3fff");
	}
	
}	 
*/

